package org.gw.building.repositories;

import org.gw.building.model.Bm;
import org.gw.building.model.Tyxxb;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

/**
 * Created by qwx89 on 2017/7/30.
 */
public interface TyxxbRepository extends CrudRepository<Tyxxb, String> {
    @Query("from Tyxxb t where t.yx = ?1 and t.zbmc.id = ?2 and t.zbsj = 1")
    Tyxxb findZbsjByBranch(String departmentId, String bmId);

    @Modifying
    @Query("delete from Tyxxb t where t.yx = ?1 ")
    void deleteByYx(String yx);

    @Query("from Tyxxb t where (?1 is null or t.xh like ?1 or t.xm like ?1) and (?2 is null or t.tgb = ?2) and (?3 is null or t.yx = ?3) and t.scbz = 0 and t.lx.dm = 2")
    Page<Tyxxb> queryTgb(String s, String lx, String yx, Pageable pageable);

    @Query("from Tyxxb t where t.xh = ?1")
    Tyxxb findZbsjByXh(String zbsjXh);

    @Query("from Tyxxb t where t.zbmc = ?1 and t.zbsj = 0 and t.scbz = 0 and t.sjly.dm = 1")
    List<Tyxxb> findZBTyxx(Bm zb);

    List<Tyxxb> findByZbmcAndScbz(Bm bm,boolean scbz);

    @Modifying(clearAutomatically = true)
    @Query("delete from Tyxxb t where t.xh = ?1")
    void deleteByXh(String xh);
}
